package pl.lodz.p.kipme;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.jws.WebService;

import org.springframework.stereotype.Service;

import pl.lodz.p.kipme.bo.Category;
import pl.lodz.p.kipme.bo.Order;
import pl.lodz.p.kipme.bo.Product;
import pl.lodz.p.kipme.bo.ProductInfo;
import pl.lodz.p.kipme.bo.ProductOrder;
import pl.lodz.p.kipme.bo.ProductType;
import pl.lodz.p.kipme.bo.Transaction;
import pl.lodz.p.kipme.dao.OrderDAO;
import pl.lodz.p.kipme.dao.ProductDAO;
import pl.lodz.p.kipme.dao.TransactionDAO;
import pl.lodz.p.kipme.exception.BusinessException;

@WebService(endpointInterface = "pl.lodz.p.kipme.MainService", serviceName = "mainService")
public class MainServiceORM implements MainService {
    private ProductDAO productDAO;
    private TransactionDAO transactionDAO;
    private OrderDAO orderDAO;

    @Override
    public List<Category> getCategoryList() {
        return productDAO.getCategories();
    }

    @Override
    public List<ProductType> getProductTypeList() {
        return productDAO.getProductTypes();
    }
    
    @Override
    public void addProduct(Product product) throws BusinessException{
    	productDAO.addProduct(product);
    }
    
    @Override
    public void updateProduct(Product product) throws BusinessException{
    	productDAO.updateProduct(product);
    }
    
    @Override
    public void removeProduct(String productCode){
    	productDAO.removeProduct(productCode);
    }

    @Override
    public List<ProductInfo> getProductInfo() {
        return productDAO.getProductInfoList();
    }

    @Override
    public Product getProductByProductCode(String productCode) {
        return productDAO.getProductByCode(productCode);
    }

    @Override
    public List<Product> getProductsByCategory(String categoryCode) {
        return productDAO.getProductsByCategory(categoryCode);
    }

    @Override
    public List<Product> getProductsByType(String typeCode) {
        return productDAO.getProductsByType(typeCode);
    }

    @Override
    public void reportSale(Transaction transaction) {
    	transactionDAO.addTransaction(transaction);
    	productDAO.updateNumberOfProduct(transaction.getProductCode(), (-1)*transaction.getNumOfProducts());
    }

    @Override
    public void placeOrder(Order order) throws BusinessException {
    	orderDAO.addOrder(order);
    }

    @Override
    public List<ProductOrder> checkOrderStatus(String orderId) {
        return orderDAO.checkOrderStatus(orderId);
    }

    @Override
    public List<String> getActiveOrderId() {
        return orderDAO.getActiveOrderId();
    }

    @Override
    public void confirmProductReceive(String productOrderId) throws BusinessException {
    	orderDAO.confirmProductReceive(productOrderId);
    	ProductOrder productOrder = orderDAO.getProductOrderByBusinessID(productOrderId);
    	productDAO.updateNumberOfProduct(productOrder.getProductId(), productOrder.getNumberOfProduct());
    }

    @Override
    public void updateNumberOfProduct(String productCode, String newAmount) {
    	productDAO.updateNumberOfProduct(productCode, Integer.valueOf(newAmount));
    }

    @Override
    public int getNumberOfProduct(String productCode) {
        return productDAO.countProduct(productCode);
    }

    @Override
    public List<Transaction> getTransactions(Date startDate, Date endDate) {
        return transactionDAO.getTransactionsBetweenDates(startDate, endDate);
    }

    @Override
    public List<Transaction> getTransactionsByProductCode(String productCode) {
        return transactionDAO.getTransactionsByProductCode(productCode);
    }

    public ProductDAO getProductDAO() {
        return productDAO;
    }

    public void setProductDAO(ProductDAO productDAO) {
        this.productDAO = productDAO;
    }

	public TransactionDAO getTransactionDAO() {
		return transactionDAO;
	}

	public void setTransactionDAO(TransactionDAO transactionDAO) {
		this.transactionDAO = transactionDAO;
	}

	public OrderDAO getOrderDAO() {
		return orderDAO;
	}

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

}
